using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using ns20;

namespace ns21
{
	internal class Class131
	{
		[CompilerGenerated]
		private sealed class Class132
		{
			public JsonSchemaType jsonSchemaType_0;

			public bool method_0(KeyValuePair<string, JsonSchemaType> keyValuePair_0)
			{
				return keyValuePair_0.Value == jsonSchemaType_0;
			}
		}

		private readonly IList<JsonSchema> ilist_0;

		private readonly JsonSchemaResolver jsonSchemaResolver_0;

		private readonly IDictionary<string, JsonSchema> idictionary_0;

		private JsonSchema jsonSchema_0;

		private JObject jobject_0;

		private JsonSchema JsonSchema_0 => jsonSchema_0;

		public Class131(JsonSchemaResolver resolver)
		{
			ilist_0 = new List<JsonSchema>();
			idictionary_0 = new Dictionary<string, JsonSchema>();
			jsonSchemaResolver_0 = resolver;
		}

		private void method_0(JsonSchema jsonSchema_1)
		{
			jsonSchema_0 = jsonSchema_1;
			ilist_0.Add(jsonSchema_1);
			jsonSchemaResolver_0.LoadedSchemas.Add(jsonSchema_1);
			idictionary_0.Add(jsonSchema_1.String_0, jsonSchema_1);
		}

		private JsonSchema method_1()
		{
			JsonSchema result = jsonSchema_0;
			ilist_0.RemoveAt(ilist_0.Count - 1);
			jsonSchema_0 = ilist_0.LastOrDefault();
			return result;
		}

		internal JsonSchema method_2(JsonReader jsonReader_0)
		{
			JToken jToken = JToken.ReadFrom(jsonReader_0);
			jobject_0 = jToken as JObject;
			JsonSchema jsonSchema = method_5(jToken);
			method_4(jsonSchema);
			return jsonSchema;
		}

		private string method_3(string string_0)
		{
			return Uri.UnescapeDataString(string_0).Replace("~1", "/").Replace("~0", "~");
		}

		private JsonSchema method_4(JsonSchema jsonSchema_1)
		{
			if (jsonSchema_1.String_2 != null)
			{
				string text = jsonSchema_1.String_2;
				bool flag;
				if (flag = text.StartsWith("#", StringComparison.OrdinalIgnoreCase))
				{
					text = method_3(text);
				}
				JsonSchema jsonSchema = jsonSchemaResolver_0.GetSchema(text);
				if (jsonSchema == null)
				{
					if (flag)
					{
						string[] array = jsonSchema_1.String_2.TrimStart('#').Split(new char[1] { '/' }, StringSplitOptions.RemoveEmptyEntries);
						JToken jToken = jobject_0;
						string[] array2 = array;
						foreach (string string_ in array2)
						{
							string text2 = method_3(string_);
							if (jToken.Type == JTokenType.Object)
							{
								jToken = jToken[text2];
							}
							else if (jToken.Type == JTokenType.Array || jToken.Type == JTokenType.Constructor)
							{
								jToken = ((!int.TryParse(text2, out var result) || result < 0 || result >= jToken.Count()) ? null : jToken[result]);
							}
							if (jToken == null)
							{
								break;
							}
						}
						if (jToken != null)
						{
							jsonSchema = method_5(jToken);
						}
					}
					if (jsonSchema == null)
					{
						throw new JsonException("Could not resolve schema reference '{0}'.".smethod_0(CultureInfo.InvariantCulture, jsonSchema_1.String_2));
					}
				}
				jsonSchema_1 = jsonSchema;
			}
			if (jsonSchema_1.Boolean_0)
			{
				return jsonSchema_1;
			}
			jsonSchema_1.Boolean_0 = true;
			if (jsonSchema_1.Extends != null)
			{
				for (int j = 0; j < jsonSchema_1.Extends.Count; j++)
				{
					jsonSchema_1.Extends[j] = method_4(jsonSchema_1.Extends[j]);
				}
			}
			if (jsonSchema_1.Items != null)
			{
				for (int k = 0; k < jsonSchema_1.Items.Count; k++)
				{
					jsonSchema_1.Items[k] = method_4(jsonSchema_1.Items[k]);
				}
			}
			if (jsonSchema_1.AdditionalItems != null)
			{
				jsonSchema_1.AdditionalItems = method_4(jsonSchema_1.AdditionalItems);
			}
			if (jsonSchema_1.PatternProperties != null)
			{
				foreach (KeyValuePair<string, JsonSchema> item in jsonSchema_1.PatternProperties.ToList())
				{
					jsonSchema_1.PatternProperties[item.Key] = method_4(item.Value);
				}
			}
			if (jsonSchema_1.Properties != null)
			{
				foreach (KeyValuePair<string, JsonSchema> item2 in jsonSchema_1.Properties.ToList())
				{
					jsonSchema_1.Properties[item2.Key] = method_4(item2.Value);
				}
			}
			if (jsonSchema_1.AdditionalProperties != null)
			{
				jsonSchema_1.AdditionalProperties = method_4(jsonSchema_1.AdditionalProperties);
			}
			return jsonSchema_1;
		}

		private JsonSchema method_5(JToken jtoken_0)
		{
			JObject jObject = jtoken_0 as JObject;
			if (jObject == null)
			{
				throw JsonException.smethod_0(jtoken_0, jtoken_0.Path, "Expected object while parsing schema object, got {0}.".smethod_0(CultureInfo.InvariantCulture, jtoken_0.Type));
			}
			if (jObject.TryGetValue("$ref", out var value))
			{
				JsonSchema jsonSchema = new JsonSchema();
				jsonSchema.String_2 = (string)value;
				return jsonSchema;
			}
			string text = jtoken_0.Path.Replace(".", "/").Replace("[", "/").Replace("]", string.Empty);
			if (!string.IsNullOrEmpty(text))
			{
				text = "/" + text;
			}
			text = "#" + text;
			if (idictionary_0.TryGetValue(text, out var value2))
			{
				return value2;
			}
			method_0(new JsonSchema
			{
				String_0 = text
			});
			method_6(jObject);
			return method_1();
		}

		private void method_6(JObject jobject_1)
		{
			foreach (KeyValuePair<string, JToken> item in jobject_1)
			{
				switch (item.Key)
				{
				case "type":
					JsonSchema_0.Type = method_13(item.Value);
					break;
				case "id":
					JsonSchema_0.Id = (string)item.Value;
					break;
				case "title":
					JsonSchema_0.Title = (string)item.Value;
					break;
				case "description":
					JsonSchema_0.Description = (string)item.Value;
					break;
				case "properties":
					JsonSchema_0.Properties = method_11(item.Value);
					break;
				case "items":
					method_12(item.Value);
					break;
				case "additionalProperties":
					method_9(item.Value);
					break;
				case "additionalItems":
					method_10(item.Value);
					break;
				case "patternProperties":
					JsonSchema_0.PatternProperties = method_11(item.Value);
					break;
				case "required":
					JsonSchema_0.Required = (bool)item.Value;
					break;
				case "requires":
					JsonSchema_0.Requires = (string)item.Value;
					break;
				case "minimum":
					JsonSchema_0.Minimum = (double)item.Value;
					break;
				case "maximum":
					JsonSchema_0.Maximum = (double)item.Value;
					break;
				case "exclusiveMinimum":
					JsonSchema_0.ExclusiveMinimum = (bool)item.Value;
					break;
				case "exclusiveMaximum":
					JsonSchema_0.ExclusiveMaximum = (bool)item.Value;
					break;
				case "maxLength":
					JsonSchema_0.MaximumLength = (int)item.Value;
					break;
				case "minLength":
					JsonSchema_0.MinimumLength = (int)item.Value;
					break;
				case "maxItems":
					JsonSchema_0.MaximumItems = (int)item.Value;
					break;
				case "minItems":
					JsonSchema_0.MinimumItems = (int)item.Value;
					break;
				case "divisibleBy":
					JsonSchema_0.DivisibleBy = (double)item.Value;
					break;
				case "disallow":
					JsonSchema_0.Disallow = method_13(item.Value);
					break;
				case "default":
					JsonSchema_0.Default = item.Value.DeepClone();
					break;
				case "hidden":
					JsonSchema_0.Hidden = (bool)item.Value;
					break;
				case "readonly":
					JsonSchema_0.ReadOnly = (bool)item.Value;
					break;
				case "format":
					JsonSchema_0.Format = (string)item.Value;
					break;
				case "pattern":
					JsonSchema_0.Pattern = (string)item.Value;
					break;
				case "enum":
					method_8(item.Value);
					break;
				case "extends":
					method_7(item.Value);
					break;
				case "uniqueItems":
					JsonSchema_0.UniqueItems = (bool)item.Value;
					break;
				}
			}
		}

		private void method_7(JToken jtoken_0)
		{
			IList<JsonSchema> list = new List<JsonSchema>();
			if (jtoken_0.Type == JTokenType.Array)
			{
				foreach (JToken item in (IEnumerable<JToken>)jtoken_0)
				{
					list.Add(method_5(item));
				}
			}
			else
			{
				JsonSchema jsonSchema = method_5(jtoken_0);
				if (jsonSchema != null)
				{
					list.Add(jsonSchema);
				}
			}
			if (list.Count > 0)
			{
				JsonSchema_0.Extends = list;
			}
		}

		private void method_8(JToken jtoken_0)
		{
			if (jtoken_0.Type != JTokenType.Array)
			{
				throw JsonException.smethod_0(jtoken_0, jtoken_0.Path, "Expected Array token while parsing enum values, got {0}.".smethod_0(CultureInfo.InvariantCulture, jtoken_0.Type));
			}
			JsonSchema_0.Enum = new List<JToken>();
			foreach (JToken item in (IEnumerable<JToken>)jtoken_0)
			{
				JsonSchema_0.Enum.Add(item.DeepClone());
			}
		}

		private void method_9(JToken jtoken_0)
		{
			if (jtoken_0.Type == JTokenType.Boolean)
			{
				JsonSchema_0.AllowAdditionalProperties = (bool)jtoken_0;
			}
			else
			{
				JsonSchema_0.AdditionalProperties = method_5(jtoken_0);
			}
		}

		private void method_10(JToken jtoken_0)
		{
			if (jtoken_0.Type == JTokenType.Boolean)
			{
				JsonSchema_0.AllowAdditionalItems = (bool)jtoken_0;
			}
			else
			{
				JsonSchema_0.AdditionalItems = method_5(jtoken_0);
			}
		}

		private IDictionary<string, JsonSchema> method_11(JToken jtoken_0)
		{
			IDictionary<string, JsonSchema> dictionary = new Dictionary<string, JsonSchema>();
			if (jtoken_0.Type != JTokenType.Object)
			{
				throw JsonException.smethod_0(jtoken_0, jtoken_0.Path, "Expected Object token while parsing schema properties, got {0}.".smethod_0(CultureInfo.InvariantCulture, jtoken_0.Type));
			}
			foreach (JProperty item in (IEnumerable<JToken>)jtoken_0)
			{
				if (!dictionary.ContainsKey(item.Name))
				{
					dictionary.Add(item.Name, method_5(item.Value));
					continue;
				}
				throw new JsonException("Property {0} has already been defined in schema.".smethod_0(CultureInfo.InvariantCulture, item.Name));
			}
			return dictionary;
		}

		private void method_12(JToken jtoken_0)
		{
			JsonSchema_0.Items = new List<JsonSchema>();
			switch (jtoken_0.Type)
			{
			default:
				throw JsonException.smethod_0(jtoken_0, jtoken_0.Path, "Expected array or JSON schema object, got {0}.".smethod_0(CultureInfo.InvariantCulture, jtoken_0.Type));
			case JTokenType.Object:
				JsonSchema_0.Items.Add(method_5(jtoken_0));
				JsonSchema_0.PositionalItemsValidation = false;
				break;
			case JTokenType.Array:
				JsonSchema_0.PositionalItemsValidation = true;
				foreach (JToken item in (IEnumerable<JToken>)jtoken_0)
				{
					JsonSchema_0.Items.Add(method_5(item));
				}
				break;
			}
		}

		private JsonSchemaType? method_13(JToken jtoken_0)
		{
			switch (jtoken_0.Type)
			{
			default:
				throw JsonException.smethod_0(jtoken_0, jtoken_0.Path, "Expected array or JSON schema type string token, got {0}.".smethod_0(CultureInfo.InvariantCulture, jtoken_0.Type));
			case JTokenType.String:
				return smethod_0((string)jtoken_0);
			case JTokenType.Array:
			{
				JsonSchemaType? jsonSchemaType = JsonSchemaType.None;
				{
					foreach (JToken item in (IEnumerable<JToken>)jtoken_0)
					{
						if (item.Type == JTokenType.String)
						{
							jsonSchemaType |= smethod_0((string)item);
							continue;
						}
						throw JsonException.smethod_0(item, item.Path, "Exception JSON schema type string token, got {0}.".smethod_0(CultureInfo.InvariantCulture, jtoken_0.Type));
					}
					return jsonSchemaType;
				}
			}
			}
		}

		internal static JsonSchemaType smethod_0(string string_0)
		{
			if (!Class133.idictionary_0.TryGetValue(string_0, out var value))
			{
				throw new JsonException("Invalid JSON schema type: {0}".smethod_0(CultureInfo.InvariantCulture, string_0));
			}
			return value;
		}

		internal static string smethod_1(JsonSchemaType jsonSchemaType_0)
		{
			return Class133.idictionary_0.Single((KeyValuePair<string, JsonSchemaType> keyValuePair_0) => keyValuePair_0.Value == jsonSchemaType_0).Key;
		}
	}
}
